/*
 * A72_cmd.c
 *
 *  Created on: Mar 16, 2024
 *      Author: 25119
 */

#include "A72_cmd.h"

A72_rx_buf rx_buf;

const uint8_t C_A72_T_reboot[] = {3, 0x01, 0xFF, 0x01};
const uint8_t C_A72_T_restore[] = {3, 0xE9, 0xFF, 0x02};

const uint8_t C_A72_T_read_address[] = {3, 0x01, 0xFF, 0x02};
const uint8_t C_A72_R_read_address[] = {2, 0x93, 0x02};

const uint8_t C_A72_T_read_program_info[] = {3, 0x01, 0xFF, 0x03};
const uint8_t C_A72_R_read_program_info[] = {2, 0x93, 0x03};

const uint8_t C_A72_T_read_net_statue[] = {3, 0x01, 0xFF, 0x04};
const uint8_t C_A72_R_read_net_statue[] = {2, 0x93, 0x04};

const uint8_t C_A72_T_get_gateway_info[] = {3, 0xE9, 0xFF, 0x0C};
const uint8_t C_A72_R_get_gateway_info[] = {2, 0x91, 0x8B};

const uint8_t C_A72_T_set_gateway_network_key = 4;
uint8_t C_A72_T_set_gateway_info[] =
{
    28, 0xE9, 0xFF, 0x09,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0x00, 0x00, 0x00, 0x11, 0x22, 0x33, 0x44, 0x01, 0x00,
};

const uint8_t C_A72_T_start_scan[] = {3, 0xE9, 0xFF, 0x00};
const uint8_t C_A72_R_start_scan[] = {2, 0x91, 0x88};

const uint8_t C_A72_T_stop_scan[] = {3, 0xE9, 0xFF, 0x01};

const uint8_t C_A72_T_filter_addr[] = {3, 0xE9, 0xFF, 0x08};

const uint8_t C_A72_T_node_param_cfg_1[] = {3, 0xE9, 0xFF, 0x0A};
const uint8_t C_A72_T_node_param_cfg_2[] = {7, 0x00, 0x00, 0x00, 0x11, 0x22, 0x33, 0x44};
const uint8_t C_A72_R_node_param_cfg[] = {2, 0x91, 0x89};

const uint8_t C_A72_T_node_bind[] = {6, 0xE9, 0xFF, 0x0B, 0x00, 0x00, 0x00};
const uint8_t C_A72_R_node_bind[] = {2, 0x91, 0x82};

const uint8_t C_A72_T_delete_node_1[] = {8, 0xE8, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00};
const uint8_t C_A72_T_delete_node_2[] = {2, 0x80, 0x49};
const uint8_t C_A72_R_delete_node_1[] = {2, 0x91, 0x81};
const uint8_t C_A72_R_delete_node_2[] = {4, 0x01, 0x00, 0x80, 0x4A};

const uint8_t C_A72_T_group_addr_1 = 9;
const uint8_t C_A72_T_group_op_code = 12;
const uint8_t C_A72_T_add_group_op_code = 0x1B;
const uint8_t C_A72_T_delete_group_op_code = 0x1C;
const uint8_t C_A72_T_delete_all_group_op_code = 0x1D;
const uint8_t C_A72_T_group_addr_2 = 13;
const uint8_t C_A72_T_group_group = 15;
const uint8_t C_A72_T_group_others = 16;
const uint8_t C_A72_T_group_others_1[] = {3, 0xC0, 0x00, 0x10}; //add or delete
const uint8_t C_A72_T_group_others_2[] = {2, 0x00, 0x10}; //delete all
uint8_t C_A72_T_group[] = {18, 0xE8, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x02, 0x01, 0, 0, 0x80, 0, 0, 0, 0, 0, 0, 0};

const uint8_t C_A72_T_uart_tx[] = {2, 0x00, 0xFF};

const uint8_t C_A72_R_gateway_uart_rx_1[] = {2, 0x91, 0x81};
const uint8_t C_A72_R_gateway_uart_rx_2[] = {3, 0x01, 0x00, 0x52};

const uint8_t C_A72_R_node_uart_rx[] = {2, 0x93, 0x00};

inline int A72_tx_cmd(UART_HandleTypeDef *huart, const uint8_t *cmd)
{
  return HAL_UART_Transmit(huart, cmd + 1, *cmd, UART_TIMEOUT);
}

inline int A72_rx_cmd(UART_HandleTypeDef *huart, uint8_t *cmd)
{
  uint16_t head;

  if (HAL_UART_Receive(huart, (uint8_t*)(&head), 2, UART_TIMEOUT) != HAL_OK)
    return -1;

  if (HAL_UART_Receive(huart, cmd, head, UART_TIMEOUT) != HAL_OK)
    return -2;

  return 0;
}

inline int A72_cmd_cmp(uint8_t *cmd_rx, const uint8_t *cmd_std)
{
  return memcmp(cmd_rx, cmd_std + 1, *cmd_std);
}

//static uint16_t head;
//static uint8_t flag = 0;
//static uint8_t *temp;
//int rx_cmd_handle(UART_HandleTypeDef *huart)
//{
////  if (flag == 1)
////  {
////    HAL_UART_Receive_IT(huart, temp, head);
////    flag = 2;
////  }
////  else if (flag == 2)
////  {
////    flag = 3;
////  }
////  else
////  {
////    HAL_UART_AbortReceive(huart);
////  }
//
//  return 0;
//}

//int A72_rx_cmd_it(UART_HandleTypeDef *huart, uint8_t *cmd, uint32_t time_out)
//{
//  if (HAL_UART_Receive_IT(huart, (uint8_t*)(&head), 2) != HAL_OK)
//    return -1;
//
//  bool head_get = false;
//  flag = 1;
//  temp = cmd;
//
//  for (int i = 0; i < (time_out / 10); i++)
//  {
//    if (head)
//    {
//      if (head_get == false && flag == 2)
//      {
//        i = 0;
//        head_get = true;
//      }
//      else if (flag == 3)
//      {
//        flag = 0;
//        HAL_UART_AbortReceive(huart);
//        return 0;
//      }
//    }
//    DELAY_MS(10);
//  }
//
//  HAL_UART_AbortReceive(huart);
//  return -2;
//}

int A72_rx_cmd_long_time(UART_HandleTypeDef *huart, uint8_t *cmd, uint32_t time_out)
{
  uint16_t head;
  int ret = 0;
  HAL_StatusTypeDef flag = HAL_UART_Receive(huart, (uint8_t*)(&head), 2, time_out);
  if (flag != HAL_OK)
  {
    ret = -1;
    goto stop_and_return;
  }
  flag = HAL_UART_Receive(huart, cmd, head, time_out);
  if (flag != HAL_OK)
  {
    ret = -2;
    goto stop_and_return;
  }
  return 0;

  stop_and_return:
  HAL_UART_AbortReceive(huart);
  return ret;
}

int A72_tx_uart(UART_HandleTypeDef *huart, uint16_t addr, uint8_t *p_data, uint8_t len, uint32_t time_out)
{
  uint8_t temp[64];
  uint8_t *p_data0 = temp;
  uint16_t len0 = C_A72_T_uart_tx[0] + 2 + len;
  memcpy(p_data0, C_A72_T_uart_tx + 1, C_A72_T_uart_tx[0]);
  p_data0 += C_A72_T_uart_tx[0];
  *p_data0 = *((uint8_t*)&addr + 1);
  *(p_data0 + 1) = *((uint8_t*)&addr + 0);
  p_data0 += 2;
  memcpy(p_data0, p_data, len);
  HAL_UART_Transmit(huart, temp, len0, time_out);
  return 0;

//  HAL_UART_Transmit(huart, C_A72_T_uart_tx + 1, C_A72_T_uart_tx[0], time_out);
//  HAL_UART_Transmit(huart, (uint8_t*)&addr + 1, 1, time_out);
//  HAL_UART_Transmit(huart, (uint8_t*)&addr + 0, 1, time_out);
//  HAL_UART_Transmit(huart, p_data0, len, time_out);
//  return 0;
}
